#!/bin/bash
#
# Copyright (c) 2021 NVI, Inc.
#
# This file is part of VLBI Field System
# (see http://github.com/nvi-inc/fs).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# sample script to add printing print summary and .prc
# can be customized to print more or different files.
# use -e to make sure we exit on fesh error including
#  if $STATION not set

set -e

FS_BASE=${FS_BASE:-'/usr2'}
CONTROL_DIR=${CONTROL_DIR:-"${FS_BASE}/control"}
if [[ -e $CONTROL_DIR/skedf.ctl ]]; then
    proc_dir=$( perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$proc/i     ) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
    snap_dir=$( perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$snap/i     ) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
    sched_dir=$(perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$schedules/i) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
fi

fesh -P "$@"
lpr "$proc_dir/${@: -1}$STATION.prc"
